<template>
  <div class="home-container">
    <el-container class="home">
      <!-- 头部区域 -->
      <el-header>
        <div>
          <div class="homelogo">
            <!-- <img src="../assets/homelogo.png" alt="" /> -->
          </div>
          <span>电商后台管理系统</span>
        </div>
        <el-button @click="logout" class="btn">退出登录</el-button>
      </el-header>
      <!-- 页面主体区 -->
      <el-container class="bottom">
        <!-- 侧边栏 -->
        <el-aside :width="isFlod ? '64px' : '200px'">
          <!-- 折叠菜单按钮 -->
          <div class="totlgebtn" @click="totlgebtn">|||</div>
          <!-- 侧边兰菜单区域 -->
          <el-menu
            background-color="#3b444b"
            text-color="#fff"
            active-text-color="#ee554f"
            unique-opened
            :collapse="isFlod"
            :collapse-transition="false"
            router
            :default-active="activePath"
          >
            <el-menu-item
              index="/welcome"
              popper-class="www"
              class="index"
              :class="{ inactive: 100 === currentIndex }"
              @click.native="setColor(100)"
            >
              <!-- 一级菜单模板区域 -->
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-s-home"></i>
                <!-- 文本 -->
                <span>首页</span>
              </template>
            </el-menu-item>
            <!-- 一级菜单 -->
            <el-submenu
              popper-class="www"
              :index="item.id + ''"
              v-for="(item, index) in menulist"
              :key="item.id"
              :class="{ ddd: index === currentIndex }"
              @click.native="setColor(index)"
            >
              <!-- 一级菜单模板区域 -->
              <template slot="title" class="el-submenu__titl">
                <!-- 图标 -->
                <i ref="ddd" :class="'iconfont ' + iconobj[item.id]"></i>
                <!-- 文本 -->
                <span>{{ item.authName }}</span>
              </template>
              <!-- 二级菜单 -->
              <el-menu-item
                :index="'/' + subitem.path"
                v-for="subitem in item.children"
                :key="subitem.id"
              >
                <!-- 二级菜单模板区域 -->
                <template slot="title">
                  <!-- 图标 -->
                  <i class="el-icon-menu"></i>
                  <!-- 文本 -->
                  <span>{{ subitem.authName }}</span>
                </template>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <!-- 右侧内容主体区 -->
        <el-main>
          <!-- 占位符 -->
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>
<script>
export default {
  data () {
    return {
      currentIndex: '',
      //  左侧菜单数据
      menulist: [],
      //  图标对象
      iconobj: {
        125: 'icon-users',
        103: 'icon-tijikongjian',
        101: 'icon-shangpin',
        102: 'icon-danju',
        145: 'icon-baobiao'
      },
      //  菜单是否折叠
      isFlod: false,
      // 保存高亮显示path
      activePath: ''
    }
  },
  created () {
    //  页面一加载就获取所有的菜单数据
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    //  退出登录
    logout () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    //  获取菜单数据
    async getMenuList () {
      const { data: res } = await this.$http.get('menus')
      //  获取失败 弹窗提示
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      //  获取数据成功赋值给 menulist
      this.menulist = res.data
    },
    //  折叠菜单按钮事件
    totlgebtn () {
      this.isFlod = !this.isFlod
    },
    setColor (i) {
      this.currentIndex = i
    }
  }
}
</script>
<style lang="less" scoped>
.el-header {
  background: linear-gradient(to right, #f63c2f, #813af1);
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-left: 0;
  color: #fff;
  font-size: 20px;
  font-weight: 700;
  padding-left: 10px;
  div {
    display: flex;
    align-items: center;
    img {
      width: 120px;
      height: 90px;
      margin-right: 20px;
    }
  }
}
.btn {
  background-color: #f63c2f;
  color: #fff;
}
.el-aside {
  background-color: #3b444b;
  transition: 0.5s all;
  .el-menu {
    border: none;
    .el-submenu {
      background-color: red;
    }
  }
}
.el-main {
  background-color: #eaedf1;
}
.home-container,
.home,
.el-container {
  height: 100%;
  width: 100%;
}
.iconfont {
  margin-right: 10px;
  color: #fff;
}
.totlgebtn {
  background-color: #3b444b;
  font-size: 16px;
  text-align: center;
  line-height: 32px;
  color: #fff;
  letter-spacing: 0.2em;
  cursor: pointer;
}
.bottom {
  height: 90%;
}
</style>
<style lang="less">
.ddd .el-submenu__title {
  background-color: #ea3c43 !important;
}
// .is-active {
//  background-color: #ea3c43 !important;
// }
.ddd .el-submenu__title:hover {
  background-color: #ea3c43 !important;
}
.el-menu-vertical-demo:not(.el-menu--collapse) {
  width: 200px;
  min-height: 400px;
}
//  .ddd .el-menu-item:hover {
//      background-color: #ea3c43 !important;

//   }
.index {
  color: #fff !important;
}
.inactive {
  background-color: #ea3c43 !important ;
}
.el-icon-s-home {
  color: #fff !important;
}
.indexh {
  z-index: 99999;
}
</style>
